package user

import (
	"github.com/gin-gonic/gin"
	"likeadmin/client/schemas/req"
	"likeadmin/client/service/user"
	"likeadmin/config"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

const PaySuccessResponse = "SUCCESS"

var UserGroup = core.Group("/user", newAdminHandler, regAdmin, middleware.TokenAuthClient())

func newAdminHandler(srv user.ISystemAuthAdminService) *adminHandler {
	return &adminHandler{srv: srv}
}

func regAdmin(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *adminHandler) {
		rg.POST("/self", handle.self)
		rg.POST("/drama/list", handle.dramaList)
		rg.POST("/drama/look", handle.dramaLook)
		rg.POST("/series/list", handle.seriesList)
		rg.POST("/recharge/gear", handle.rechargeGear)
		rg.POST("/read/records", handle.readRecords)
		rg.POST("/pay/send", handle.paySend)
		rg.GET("/pay/notify/:channel", handle.payCall)
		rg.POST("/order/is_pay", handle.orderIsPay)
		rg.POST("/order/list", handle.orderList)
		rg.POST("/consum/list", handle.consumList)
	})
}

type adminHandler struct {
	srv user.ISystemAuthAdminService
}

// self 当前用户信息
func (ah *adminHandler) self(c *gin.Context) {
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.Self(c, adminId)
	response.CheckAndRespWithData(c, res, err)
}

// dramaList 短剧列表
func (ah *adminHandler) dramaList(c *gin.Context) {
	var page request.PageJsonReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &page)) {
		return
	}
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.DramaList(adminId, page.PageNo, page.PageSize)
	response.CheckAndRespWithData(c, res, err)
}

// dramaLook 看视频
func (ah *adminHandler) dramaLook(c *gin.Context) {
	var dramaLookReq req.DramaLookReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &dramaLookReq)) {
		return
	}
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.DramaLook(adminId, dramaLookReq)
	response.CheckAndRespWithData(c, res, err)
}

// seriesList 剧集列表
func (ah *adminHandler) seriesList(c *gin.Context) {
	var page request.PageJsonReq
	var seriesReq req.SeriesReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &seriesReq)) {
		return
	}
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.SeriesList(adminId, page.PageNo, page.PageSize, seriesReq)
	response.CheckAndRespWithData(c, res, err)
}

// rechargeGear 充值档位
func (ah *adminHandler) rechargeGear(c *gin.Context) {
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.RechargeGear(adminId)
	response.CheckAndRespWithData(c, res, err)
}

// readRecords 阅读记录
func (ah *adminHandler) readRecords(c *gin.Context) {
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.ReadRecords(adminId)
	response.CheckAndRespWithData(c, res, err)
}

// paySend 发起支付
func (ah *adminHandler) paySend(c *gin.Context) {
	var paySendReq req.PaySendReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &paySendReq)) {
		return
	}
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.PaySend(c, adminId, paySendReq)
	response.CheckAndRespWithData(c, res, err)
}

// payCall 支付回调
func (ah *adminHandler) payCall(c *gin.Context) {
	var payNotifyReq req.PayNotifyReq
	payNotifyReq.Channel = c.Param("channel")
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &payNotifyReq)) {
		return
	}
	res, err := ah.srv.PayCall(c, payNotifyReq)
	if err != nil || res != PaySuccessResponse {
		response.CheckAndRespWithData(c, res, err)
		return
	}
	c.String(200, PaySuccessResponse)
}

// orderIsPay 订单是否支付
func (ah *adminHandler) orderIsPay(c *gin.Context) {
	var orderIsPayReq req.OrderIsPayReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &orderIsPayReq)) {
		return
	}
	res, err := ah.srv.OrderIsPay(c, orderIsPayReq)
	response.CheckAndRespWithData(c, res, err)
}

// orderList 订单列表
func (ah *adminHandler) orderList(c *gin.Context) {
	var page request.PageJsonReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &page)) {
		return
	}
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.OrderList(adminId, page.PageNo, page.PageSize)
	response.CheckAndRespWithData(c, res, err)
}

// consumList 消费记录列表
func (ah *adminHandler) consumList(c *gin.Context) {
	var page request.PageJsonReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &page)) {
		return
	}
	adminId := config.ClientConfig.GetAdminId(c)
	res, err := ah.srv.ConsumList(adminId, page.PageNo, page.PageSize)
	response.CheckAndRespWithData(c, res, err)
}
